

//
//  ybsearchdetialViewController.m
//  OurTourism
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "ybsearchdetialViewController.h"
#import "Header.h"
#import "YBDestinationModel.h"
#import "YBsearchTableViewCell.h"

#import "YBTraveldetialViewController.h"
@interface ybsearchdetialViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UISearchBar *searchBar;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIView *titleView;
@property (nonatomic, retain)NSMutableArray *dataArr; //保存总数据
@property (nonatomic, strong)MBProgressHUD *hud;
@end

@implementation ybsearchdetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"fanhui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToLast)];
    
   // [self addScreenEdgePan];
    [self SearchBar];
    [self createTableView];
    [self getData:self.text];
    [self loadMBProgress];
}

// 小菊花
- (void)loadMBProgress
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
    self.hud.dimBackground = NO;
    //设置对话框文字
    self.hud.labelText = @"loading";
    self.hud.minShowTime = 50;
    self.hud.activityIndicatorColor = [UIColor whiteColor];
    self.hud.labelColor = [UIColor whiteColor];
    
    [self.view addSubview:self.hud];
    [self.hud showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(1);
    } completionBlock:^{
        //操作执行完后取消对话框
        [self.hud removeFromSuperview];
        self.hud = nil;
    }];
    
}



#pragma mark - 创建searchBar
- (void)SearchBar
{
    
    // 初始化
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    // 设置代理
    self.searchBar.delegate = self;
    //类型
    [self.searchBar setBarStyle:UIBarStyleDefault];
    //提示文本
    self.searchBar.placeholder = @"搜索目的地  游记...";
    
    self.searchBar.text = self.text;
    
    //键盘类型
    [self.searchBar setKeyboardType:UIKeyboardTypeDefault];
    
    [self.searchBar becomeFirstResponder];
    //视图剪切
    self.searchBar.layer.masksToBounds = YES;
    self.searchBar.layer.cornerRadius = 10;
    
    //关闭用户交互
    self.searchBar.userInteractionEnabled = YES;
    self.navigationItem.titleView =  self.searchBar;
    
    
    
}

#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, WIDTH, HEIGTH ) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
   
}

#pragma mark - 按钮触发方法
- (void)backToLast
{
   
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


#pragma mark - searchBar 触发方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    [self getData:self.searchBar.text];
}

#pragma mark - 获取搜索数据
- (void)getData:(NSString *)text
{
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/search/?key=%@&start=0&count=20&source=search",text];
    
    // 中文转码
    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [MZNetHandle getUrl:urlStr cookie:nil result:^(id result) {
        
        self.dataArr =  [YBDestinationModel transformWithArray:result[@"trips"]];
        
        if (self.dataArr.count == 0 &&![self.searchBar.text isEqualToString:@""]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有收索内容" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self performSelector:@selector(cancelAlertAction) withObject:alert afterDelay:1.0];
        }
        
    
        
        if (self.dataArr.count != 0) {
            [self createTableView];
        }
     
        [self.hud removeFromSuperview];
    }];
}

- (void)cancelAlertAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBsearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {    
        cell = [[YBsearchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    cell.str = self.searchBar.text;
   // cell.str = self.text;
    cell.recommentModel = self.dataArr[indexPath.row];
    
    
    
    return cell;
    
}

#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YBTraveldetialViewController *detail = [[YBTraveldetialViewController alloc]init];
    
    detail.locationImpressionToDetailID = (NSNumber *)[self.dataArr[indexPath.row] id];
    
    NSLog(@"%@", detail.locationImpressionToDetailID);
    [self.navigationController pushViewController:detail animated:YES];

    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

//#pragma mark-滑动手势1
//- (void)addScreenEdgePan
//{
//    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
//    screenEdgePan.edges = UIRectEdgeLeft;
//    [self.view addGestureRecognizer:screenEdgePan];
//    [self.tableView addGestureRecognizer:screenEdgePan];
//   
//}
#pragma mark - 空白处 键盘回收
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
